
var db = require('../db/db.js');

var model = {
    tablename: "system_dic",
    add: async function (obj) {

        var sql = "insert into  " + this.tablename + "(title,value,createtime,content,descs,state)"
            + " values(?,?,?,?,?,?)";
        var results = await db.execute(sql, [obj.title, obj.value, obj.createtime, obj.content, obj.descs,obj.state]);
        console.log("results", results);
        return results;

    },
    edit: async function (obj) {
        var sql = "update  " + this.tablename + " set title=?,value=?,createtime=?,content=?,descs=?,state=? where id=?";
        var results = await db.execute(sql, [obj.title, obj.value, obj.createtime, obj.content, obj.descs,obj.state, obj.id]);
        console.log("results", results);
        return results;

    },
    del: async function (where) {
        var sql = "delete from  " + this.tablename + " where " + where;
        var results = await db.execute(sql, []);
        return results;

    },
    getlist: async function (rows, first, where, join = "", order = " order by  id desc", field = "*") {
        var sql = "select " + field + "  from  " + this.tablename + " " + join + "  where " + where + " " + order + " limit " + rows + " offset  " + first;
        var results = await db.query(sql, []);
        return results;

    },
    getcount: async function (where, join = "") {
        var countinfo = await db.query("select count(*) as num  from " + this.tablename + " " + join + "   where  " + where, []);
        return countinfo[0].num;
    },
    summoney: async function (where, key) {
        var countinfo = await db.query("select  sum(" + key + ") as num  from " + this.tablename + "  where  " + where, []);
        return countinfo[0].num;
    },
    getinfo: async function (where, order = "order by id desc") {
        var sql = "select *  from  " + this.tablename + " where " + where + " " + order;
        var results = await db.query(sql, []);
        return results;

    },


}


module.exports = model;